package com.edu.service.aop;

import com.edu.service.handler.LogHandler;
import com.edu.service.impl.BookServiceImpl;
import com.edu.service.impl.UserServiceImpl;
import org.apache.commons.logging.Log;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Proxy;

/**
 * @Author:mx
 * @Date:2025/11/11-10:30:47
 */
/*
*       Bean后处理器 可以对 容器中的对象进行增强
*           实现 BeanPostProcessor
*
* */
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean.getClass() == UserServiceImpl.class || bean.getClass() == BookServiceImpl.class){
            Object proxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(),new LogHandler(bean));
            return proxy;
        }
       return bean;
    }
}
